package controllers.account;

import static play.data.Form.form;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import models.Transaction;

import play.data.Form;
import play.mvc.Result;
import prefix.integration.wm.AbstractWalletManager;
import prefix.util.OverallUtil;
import repository.ActorRepository;
import repository.OrderRepository;
import repository.OrderRepository.SummaryDataSQL;
import views.html.account_summary;
import views.html.invest_summary;
import views.html.summary;
import web.entity.User;
import web.util.ResultCode;
import controllers.BaseController;

public class Summary extends BaseController {

    final static Form<User> regForm = form(User.class);

    public static Result index() {

        return ok(account_summary.render());
    }

    public static Result invest_summary() {
        return ok(invest_summary.render());
    }

    public static Result getData() {
        ResultCode.ajaxResult ajaxResult = null;
        Map<String, String> adata = new HashMap<String, String>();
        ajaxResult = ResultCode.ajaxResult.SUCCESS;
        adata.put("avaible-cash", "9999");
        ajaxResult.setAjaxData(adata);
        return ok(ajaxResult.toJson());
    }

    private void getMoneyData(Map<String, String> adata, Integer walletId) {
        AbstractWalletManager manager = AbstractWalletManager.getInstance();
        BigDecimal availableCash = OverallUtil.round2Decimal(manager.getBalance(walletId));
        BigDecimal pendingInvestCash = OverallUtil.round2Decimal(manager.getBalancePendingInvest(walletId));
        BigDecimal pendingWithdrawlCash = OverallUtil.round2Decimal(manager.getBalancePendingWithdrawl(walletId));

        Double sumOfAddfunds = OverallUtil.round2Decimal(
                ActorRepository.getSumOfTransactions(Transaction.Type.T0_ADD_FUNDS, walletId)).doubleValue();
        Double sumOfWithdrawl = OverallUtil.round2Decimal(
                ActorRepository.getSumOfTransactions(Transaction.Type.T1_WITHDRAWL, walletId)).doubleValue();

        adata.put("availableCash", String.valueOf(availableCash));
        adata.put("pendingInvestCash", String.valueOf(pendingInvestCash));
        adata.put("pendingWithdrawlCash", String.valueOf(pendingWithdrawlCash));
        adata.put("sumOfAddfunds", String.valueOf(sumOfAddfunds));
        adata.put("sumOfWithdrawl", String.valueOf(sumOfWithdrawl));
    }

    private void getMyInvestData(Map<String, String> adata, Integer aid) {
        Map<Enum<OrderRepository.SummaryDataSQL>, Object> myInvestData = OrderRepository.getSummaryData(aid,
                SummaryDataSQL.values());
        if (myInvestData != null) {
            Double total_received_int = (Double) myInvestData.get(SummaryDataSQL.total_received_int);
            Double total_invest = (Double) myInvestData.get(SummaryDataSQL.total_invest);
            Double total_received_prncp = (Double) myInvestData.get(SummaryDataSQL.total_received_prncp);
            Double total_accrue_amount = (Double) myInvestData.get(SummaryDataSQL.total_accrue_amount);
            Double out_prncp = total_invest - total_received_prncp;
            Double total_accrue_int = total_accrue_amount - out_prncp - total_received_int;

            adata.put("total_received_int", String.valueOf(total_received_int));
            adata.put("total_invest", String.valueOf(total_invest));
            adata.put("total_received_prncp", String.valueOf(total_received_prncp));
            adata.put("total_accrue_amount", String.valueOf(total_accrue_amount));
            adata.put("out_prncp", String.valueOf(out_prncp));
            adata.put("total_accrue_int", String.valueOf(total_accrue_int));
        }
    }

}
